package com.viknix.threadpool.manager.client.enums;

import java.util.Arrays;
import java.util.Optional;

/**
 * @Author: Dongqi
 * @Date: 2021/10/26 15:31
 * @Version 1.0
 * @Description: 阻塞队列枚举
 */
public enum BlockingQueueEnum {

    /**
     * {@link java.util.concurrent.ArrayBlockingQueue}
     */
    ARRAY_BLOCKING_QUEUE(1, "ArrayBlockingQueue"),

    /**
     * {@link java.util.concurrent.LinkedBlockingQueue}
     */
    LINKED_BLOCKING_QUEUE(2, "LinkedBlockingQueue"),

    /**
     * {@link java.util.concurrent.LinkedBlockingDeque}
     */
    LINKED_BLOCKING_DEQUE(3, "LinkedBlockingDeque"),

    /**
     * {@link java.util.concurrent.DelayQueue}
     */
    DELAY_QUEUE(4, "DelayQueue"),

    /**
     * {@link java.util.concurrent.LinkedTransferQueue}
     */
    LINKED_TRANSFER_QUEUE(5, "LinkedTransferQueue"),

    /**
     * {@link java.util.concurrent.PriorityBlockingQueue}
     */
    PRIORITY_BLOCKING_QUEUE(6, "PriorityBlockingQueue"),

    /**
     * {@link java.util.concurrent.SynchronousQueue}
     */
    SYNCHRONOUS_QUEUE(7, "SynchronousQueue");

    private Integer code;
    private String name;

    BlockingQueueEnum(int code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 根据 code 返回名称
     * @return name
     */
    public static String getNameByCode(int code) {
        Optional<BlockingQueueEnum> queueTypeEnum = Arrays.stream(BlockingQueueEnum.values())
                .filter(each -> each.code == code)
                .findFirst();
        return queueTypeEnum.map(each -> each.name).orElse("");
    }

    /**
     * 根据 code 返回枚举对象
     * @return name
     */
    public static BlockingQueueEnum getEnumByCode(int code) {
        Optional<BlockingQueueEnum> queueTypeEnum = Arrays.stream(BlockingQueueEnum.values())
                .filter(each -> each.code == code)
                .findFirst();
        return queueTypeEnum.get();
    }
}
